#include "taskbyproject.h"
#include "ui_taskbyproject.h"
#include "halokpy.h"

TaskByProject::TaskByProject(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TaskByProject)
{
    ui->setupUi(parent);

    //HaloKpy *widget =(HaloKpy *)parent; ;
    //HaloKpy *widget = static_cast<HaloKpy *>(QWidget::parentWidget());
    HaloKpy *widget = static_cast<HaloKpy *>(parent);
    widget->initComboBoxData(ui->combProjectName,"项目名称","");

    QDate curdate = QDate::currentDate();
    ui->deStartDate->setMaximumDate(curdate);
    ui->deStartDate->setMinimumDate(curdate.addDays(-30));
    ui->deEndDate->setMinimumDate(curdate);
    ui->deEndDate->setMaximumDate(curdate.addDays(30));

    QDate sDate=ui->deStartDate->date();
    QDate eDate =ui->deEndDate->date();
    m_projectTaskGnt = new GanttModel() ;

    //设置列表view的表头
    QStringList headlist;
    headlist <<"任务名称"<<"类型"<<"开始日期"<<"结束日期"<<"研发人员"<<"累计耗时";
    m_projectTaskGnt->SetHorHeaderData(headlist);

    QVector<int> headListWidth = {420,40,80,80,60,60,}; // 设置列宽
    m_projectTaskGnt->SetHeaderWidth(headListWidth);

    m_projectTaskGnt->SetStartEndData(sDate,eDate);
//    QVBoxLayout *m_vlayout = new QVBoxLayout();
//    m_vlayout->addWidget(m_projectTaskGnt);
//    ui->wdTaskGnt->setLayout(m_vlayout);
    connect(m_projectTaskGnt->m_pageBar, &QPageBar::pageChanged, this, [&](int idx) {reflashPagingModelView(m_projectTaskGnt,idx);});
}

TaskByProject::~TaskByProject()
{
    delete ui;
}

void TaskByProject::reflashPagingModelView(GanttModel *prjGntModel, int curPage)
{
    prjGntModel->m_pageBar->m_currentPage = curPage;
    int modelType = prjGntModel->GetDataModelType();
    HaloKpy *widget = static_cast<HaloKpy *>(QWidget::parentWidget());
    QList<GanttTask> gntTask;
    if(modelType==1)//根据项目名称获取项目任务
        gntTask=widget->dbMySql->getTaskDataByPrjName_new(prjGntModel->m_pageBar);
    if(modelType==2)//根据开发人员姓名获取人员任务
        gntTask=widget->dbMySql->getTaskDataByDevName_new(prjGntModel->m_pageBar);
    else
        qDebug()<<"unknown datamodeltype:"<<modelType;
    prjGntModel->BindModelData(gntTask);
    prjGntModel->SetGanttModel();
}

